package com.comit.appointment.comsumer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

import java.io.IOException;
import java.util.concurrent.TransferQueue;

public class EmailMessageListener implements ChannelAwareMessageListener {

    private static final ObjectMapper MAPPER = new ObjectMapper();


    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            JsonNode jsonNode = MAPPER.readTree(message.getBody());
            String email = jsonNode.get("email").asText();
            System.out.println("获取队列中消息：" + email);
            if (email.contains("regect1")) {
                //回滚到消息队列，ack返回false，将消息返回队列，但是还是在头部  如果是消息本身问题 容易出现死循环
                //channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            }else {
                //
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }
            System.out.println("正常签收");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}